Consulte el fragmento de código. No tiene nada que ver con el concepto del acelerador.
Declaro un evento de clic como btn.addEventListener("click", outerFunction); . Se invoca, pero la declaración 'console.log ("se hace clic en el botón de la función externa" + e.pointerType)' no se ejecuta.
Mientras que si declaro la función en línea, entonces funciona como se esperaba.
Entonces no puedo entender por qué no funciona cuando se declara en outerfunction
const btn = document.querySelector("#throttle"); const btn2=document.querySelector("#newthrottle"); // Throttling Function const throttleFunction = (func, delay) => { // Previously called time of the function let prev = 0; return (...args) => { // Current called time of the function let now = new Date().getTime(); // Logging the difference between previously // called and current called timings console.log(now - prev, delay); // If difference is greater than delay call // the function again. if (now - prev > delay) { prev = now; // "..." is the spread operator here // returning the function with the // array of arguments //debugging arguments var inputArguments = [...args] console.log("arguments" + inputArguments); return func(...args); } }; }; function outerFunction(e) { console.log('Outer function') throttleFunction(((e)=>{ console.log("button is clicked outer function " + e.pointerType) }),1500) } //this does not work btn.addEventListener("click", outerFunction); //this works like a charm btn.addEventListener("click", throttleFunction((event)=>{ console.log("button is clicked throttle " + event.pointerType) }, 1500)); function onButtonClick(e){ console.log('Hello World onbutton Click' + e); } btn2.addEventListener("click", onButtonClick);Puedes hacer algo como esto -
function outerFunction(e) { console.log('Outer function') const t = throttleFunction((e)=>{ console.log("button is clicked outer function " + e.pointerType) },1500); t(); }Pero no estará definido, así que en lugar de devolver una función anónima, intente llamar a la función de devolución de llamada.